import { createAction } from 'redux-actions'
import { check, body, delJson, getJson, postJson, putJson, parseJSON, checkStatus, data } from './util'

export
const updateProfileTemplateFieldDetail = createAction('update profile template field detail')

export
const fetchProfileTemplateFieldDetail = createAction('fetch profile template field detail')

export const updateProfileTemplateFieldDetailCore = createAction('update profile template field detail',(args, cb) => 
  fetch(`/org-pick-options?configType=${args.configType}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      if (cb) cb(res.data.orgPickOptionVOs) 
      else
        return {
          ...args,
          pickOptions: res.data.orgPickOptionVOs || [],
        }
    })
)

export const updateJobField = createAction('update profile job field detail', (args, cb) => {
  const options = args.pickOptions;
  return (
    fetch(`/org-pick-options`, {
      ...putJson,
      ...body({
        orgPickOptionVOs: options,
      })
    })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(res => {
        cb && cb();
      })
  );
});

export
const clearProfileTemplateFieldDetail = createAction('clear profile template field detail')
